package com.zzyl.nursing.controller.member;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.zzyl.common.core.domain.R;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zzyl.common.annotation.Log;
import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.enums.BusinessType;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.service.IReservationService;
import com.zzyl.common.utils.poi.ExcelUtil;
import com.zzyl.common.core.page.TableDataInfo;

/**
 * 预约信息Controller
 * 
 * @author alexis
 * @date 2025-09-13
 */
@Api(tags = "预约信息管理")
@RestController
@RequestMapping("/member/reservation")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询预约信息列表
     */
    @ApiOperation("查询预约信息列表")
//    @PreAuthorize("@ss.hasPermi('member:reservation:list')")
    @GetMapping("/list")
    public TableDataInfo<List<Reservation>> list(@ApiParam("预约信息查询条件") Reservation reservation)
    {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 导出预约信息列表
     */
    @ApiOperation("导出预约信息列表")
//    @PreAuthorize("@ss.hasPermi('member:reservation:export')")
    @Log(title = "预约信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@ApiParam(value = "预约信息查询条件") HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "预约信息数据");
    }

    /**
     * 获取预约信息详细信息
     */
    @ApiOperation("获取预约信息详细信息")
//    @PreAuthorize("@ss.hasPermi('member:reservation:query')")
    @GetMapping(value = "/{id}")
    public R<Reservation> getInfo(@ApiParam("预约信息ID") @PathVariable("id") Long id)
    {
        return R.ok(reservationService.selectReservationById(id));
    }

    /**
     * 新增预约信息
     */
    @ApiOperation("新增预约信息")
//    @PreAuthorize("@ss.hasPermi('member:reservation:add')")
    @Log(title = "预约信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@ApiParam("预约信息信息") @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改预约信息
     */
    @ApiOperation("修改预约信息")
//    @PreAuthorize("@ss.hasPermi('member:reservation:edit')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@ApiParam("预约信息信息") @RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除预约信息
     */
    @ApiOperation("删除预约信息")
//    @PreAuthorize("@ss.hasPermi('member:reservation:remove')")
    @Log(title = "预约信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@ApiParam("预约信息ID数组") @PathVariable Long[] ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }


    @ApiOperation("获取当前预约取消次数")
//    @PreAuthorize("@ss.hasPermi('member:reservation:query')")
    @GetMapping(value = "/cancelled-count")
    public R<Reservation> getInfo()
    {
        return R.ok(reservationService.selectReservationCancelledCount());
    }


    /**
     * 查询每个时间段剩余预约次数
     */
    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public R<List<Map<String, Object>>> getCountByTime(
            @ApiParam("开始时间") @RequestParam(required = false) String startTime,
            @ApiParam("结束时间") @RequestParam(required = false) String endTime) {

        // 如果没有传时间参数，默认查询未来7天的数据
        LocalDateTime start = LocalDateTime.now();
        LocalDateTime end = start.plusDays(7);

        // 如果传了时间参数，则使用传入的时间
        if (StringUtils.isNotEmpty(startTime)) {
            start = LocalDateTime.parse(startTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }
        if (StringUtils.isNotEmpty(endTime)) {
            end = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        }

        List<Map<String, Object>> result = reservationService.selectCountByTime(start, end);
        return R.ok(result);
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public R<TableDataInfo<List<Reservation>>> page( )
        {
        startPage();
        List<Reservation> list = reservationService.selectReservationList(new Reservation());
        return R.ok(getDataTable(list));
    }
    /**
     * 取消预约
     */
    @ApiOperation("取消预约")
// @PreAuthorize("@ss.hasPermi('member:reservation:cancel')")
    @Log(title = "预约信息", businessType = BusinessType.UPDATE)
    @PutMapping("/{id}/cancel")
    public R<String> cancel(@ApiParam("预约ID") @PathVariable Long id) {
        try {
            String result = reservationService.cancelReservation(id);
            return R.ok(result);
        } catch (ServiceException e) {
            return R.fail(e.getMessage());
        } catch (Exception e) {
            return R.fail("取消预约失败: " + e.getMessage());
        }
    }
}
